package com.smag.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.smag.common.constant.CommentConstants;
import com.smag.common.core.domain.AjaxResult;
import com.smag.common.core.domain.entity.ArticleComment;
import com.smag.common.core.domain.entity.SysUser;
import com.smag.common.core.domain.entity.TechComment;
import com.smag.common.enums.AppHttpCodeEnum;
import com.smag.common.exception.base.BaseException;
import com.smag.common.utils.StringUtils;
import com.smag.common.utils.bean.BeanUtils;
import com.smag.system.domain.vo.ArticleCommentVo;
import com.smag.system.domain.vo.TechCommentVo;
import com.smag.system.mapper.ArticleCommentMapper;
import com.smag.system.mapper.TechCommentMapper;
import com.smag.system.service.CommentService;
import com.smag.system.service.ISysUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    private TechCommentMapper techCommentMapper;
    @Resource
    private ArticleCommentMapper articleCommentMapper;
    @Resource
    private ISysUserService userService;
    @Override
    public List<?> getTechCommentList(Integer pageNum,Integer pageSize,Long cropId) {
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<TechComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TechComment::getCropId,cropId);
        queryWrapper.eq(TechComment::getRootId, CommentConstants.ParentRootId);
        queryWrapper.orderByDesc(TechComment::getCreateTime);
        List<TechComment> rootComments = techCommentMapper.selectList(queryWrapper);
        List<TechCommentVo> cropCommentVos = toCropCommentVoList(rootComments);
        cropCommentVos
                .forEach(cropCommentVo -> cropCommentVo.setChildren(getChildren(cropCommentVo.getId())));
        return cropCommentVos;
    }
    @Transactional
    @Override
    public AjaxResult sendCropComment(TechComment cropComment) {
        if (StringUtils.isEmpty(cropComment.getContent()))
            throw new BaseException(AppHttpCodeEnum.CONTENT_NOT_NULL.getMsg());
        techCommentMapper.insert(cropComment);
        return AjaxResult.success();
    }

    @Override
    public List<?> getArticleCommentList(Integer pageNum, Integer pageSize, Long articleId) {
        PageHelper.startPage(pageNum,pageSize);
        LambdaQueryWrapper<ArticleComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleComment::getDelFlag,CommentConstants.NONE_DEL);
        queryWrapper.eq(ArticleComment::getArticleId,articleId);
        queryWrapper.eq(ArticleComment::getRootId,CommentConstants.ParentRootId);
        queryWrapper.orderByDesc(ArticleComment::getCreateTime);
        List<ArticleComment> articleComments = articleCommentMapper.selectList(queryWrapper);
        List<ArticleCommentVo> articleCommentVos = toArticleCommentVoList(articleComments);

        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        articleCommentVos.stream()
                .forEach(commentVo -> commentVo.setChildren(getChildren2(commentVo.getId())));
        articleCommentVos.stream()
                .forEach(commentVo -> commentVo.setAvatar(userService.selectUserById(commentVo.getCreateBy()).getAvatar()));

        return articleCommentVos;
    }

    @Transactional
    @Override
    public AjaxResult sendArticleComment(ArticleComment articleComment) {
        if (StringUtils.isEmpty(articleComment.getContent()))
            throw new BaseException(AppHttpCodeEnum.CONTENT_NOT_NULL.getMsg());
        articleCommentMapper.insert(articleComment);
        return AjaxResult.success();
    }

    private List<TechCommentVo> getChildren(Long cropCommentId) {
        LambdaQueryWrapper<TechComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TechComment::getRootId,cropCommentId);
        queryWrapper.orderByAsc(TechComment::getCreateTime);
        return toCropCommentVoList(techCommentMapper.selectList(queryWrapper));
    }

    private List<TechCommentVo> toCropCommentVoList(List<TechComment> list){
        List<TechCommentVo> cropCommentVos = BeanUtils.copyBeanList(list, TechCommentVo.class);
         cropCommentVos
                 .forEach(cropCommentVo -> {
                     SysUser user = userService.selectUserById(cropCommentVo.getCreateBy());
                     cropCommentVo.setUserName(user.getNickName());
                     cropCommentVo.setAvatar(user.getAvatar());
                     if (cropCommentVo.getToCommentUserId() != CommentConstants.ParentRootId)
                     cropCommentVo.setToCommentUserName(userService.selectUserById(cropCommentVo.getToCommentUserId()).getNickName());
                 });
         return cropCommentVos;
    }

    /**
     * 根据根评论的id查询所对应的子评论的集合
     * @param id 根评论的id
     * @return
     */
    private List<ArticleCommentVo> getChildren2(Long id) {

        LambdaQueryWrapper<ArticleComment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ArticleComment::getDelFlag,CommentConstants.NONE_DEL);
        queryWrapper.eq(ArticleComment::getRootId,id);
        queryWrapper.orderByDesc(ArticleComment::getCreateTime);
        List<ArticleComment> comments = articleCommentMapper.selectList(queryWrapper);
        List<ArticleCommentVo> commentVos = toArticleCommentVoList(comments);
        commentVos.stream()
                .forEach(commentVo -> commentVo.setAvatar(userService.selectUserById(commentVo.getCreateBy()).getAvatar()));
        return commentVos;
    }

    private List<ArticleCommentVo> toArticleCommentVoList(List<ArticleComment> list){
        List<ArticleCommentVo> commentVos = BeanUtils.copyBeanList(list, ArticleCommentVo.class);
        //遍历vo集合
        for (ArticleCommentVo commentVo : commentVos) {
            String nickName = userService.selectUserById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            if(commentVo.getToCommentUserId()!=-1){
                String toCommentUserName = userService.selectUserById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }
        }
        return commentVos;
    }
}


